#include "sys.h"
#include "LCD9325.h"
#include "delay.h"
/*
现在以
PA0 7为IO口
PB5 9为控制口


*/

#define LCD_RD_SET   		 GPIO_SetBits(GPIOB,GPIO_Pin_5)
#define LCD_RD_RESET  		 GPIO_ResetBits(GPIOB,GPIO_Pin_5)

#define LCD_WR_SET   		 GPIO_SetBits(GPIOB,GPIO_Pin_6)
#define LCD_WR_RESET  		 GPIO_ResetBits(GPIOB,GPIO_Pin_6)
    
#define LCD_RS_SET   		 GPIO_SetBits(GPIOB,GPIO_Pin_7) 
#define LCD_RS_RESET 		 GPIO_ResetBits(GPIOB,GPIO_Pin_7)
  
#define LCD_CS_SET   		 GPIO_SetBits(GPIOB,GPIO_Pin_8) 
#define LCD_CS_RESET  		 GPIO_ResetBits(GPIOB,GPIO_Pin_8) 
      
#define LCD_REST_SET 		 GPIO_SetBits(GPIOB,GPIO_Pin_9)
#define LCD_REST_RESET		 GPIO_ResetBits(GPIOB,GPIO_Pin_9)

void LCD_GPIO_Init()
{
	
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); 
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	
	//这里先将所有的输出口设置为推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOB,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
	//GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
	GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
}

//这个数据会根据所配置的输出数据口来进行移位
void Lcd_Writ_Bus(u16 date)
{
	/*
	这个地方会出现的问题
	1.可能io的输出能力不够
	2.输出的顺序可能反了高位在前
	
	*/
	//GPIOB->BSRR=date<<3;
	//GPIOx->BSRR = GPIO_Pin;set
	//GPIOx->BRR = GPIO_Pin;reset
	//date<<=3;
	GPIOA->BRR=0xff;
  GPIOA->BSRR=date;//直接置位
	delay_us(10);
  LCD_WR_RESET;
  LCD_WR_SET;
}


void Lcd_Write_Cmd(u16 date)  
{   
  LCD_RS_RESET;
  Lcd_Writ_Bus(date);
}

void Lcd_Write_Data(u16 date)
{
  LCD_RS_SET;
  Lcd_Writ_Bus(date);
}

void Lcd_Write_Cmd_Data(unsigned char com,unsigned char dat)
{
  Lcd_Write_Cmd(com);
  Lcd_Write_Data(dat);
}

void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2)
{
	Lcd_Write_Cmd_Data(0x2a,x1>>8);
	Lcd_Write_Cmd_Data(0x2a,x1);
	Lcd_Write_Cmd_Data(0x2a,x2>>8);
	Lcd_Write_Cmd_Data(0x2a,x2);
	Lcd_Write_Cmd_Data(0x2b,y1>>8);
	Lcd_Write_Cmd_Data(0x2b,y1);
	Lcd_Write_Cmd_Data(0x2b,y2>>8);
	Lcd_Write_Cmd_Data(0x2b,y2);
	Lcd_Write_Cmd(0x2c); 							 
}

void LCD_Init(void)
{
	
		LCD_GPIO_Init();
		LCD_REST_SET;
		delay_ms(5); 
		LCD_REST_RESET;
		delay_ms(15);
		LCD_REST_SET;
		delay_ms(15);
		LCD_CS_SET;
		LCD_WR_SET;
		LCD_CS_RESET;  //CS
    Lcd_Write_Cmd(0x01);// Software Reset
    delay_ms(20);

    Lcd_Write_Cmd(0x11);//Sleep Out
    delay_ms(50);

    Lcd_Write_Cmd(0xc2);//Power Control 3 
    Lcd_Write_Data(0x05);//APA2 APA1 APA0   Large
    Lcd_Write_Data(0x00);//Step-up cycle in Booster circuit 1 
                         //Step-up cycle in Booster circuit 2,3
    Lcd_Write_Cmd(0xc3);//Power Control 4 
    Lcd_Write_Data(0x05);//APA2 APA1 APA0   Large
    Lcd_Write_Data(0x00);//Step-up cycle in Booster circuit 1 
                         //Step-up cycle in Booster circuit 2,3
    Lcd_Write_Cmd(0xc4);//Power Control 5 
    Lcd_Write_Data(0x05);//APA2 APA1 APA0   Large
    Lcd_Write_Data(0x00);//Step-up cycle in Booster circuit 1 
                         //Step-up cycle in Booster circuit 2,3
    Lcd_Write_Cmd(0x3A); 
    Lcd_Write_Data(0x55);
    
    Lcd_Write_Cmd(0xD7); 
    Lcd_Write_Data(0x40);
    Lcd_Write_Data(0xE0);

    Lcd_Write_Cmd(0xFD);
    Lcd_Write_Data(0x06);
    Lcd_Write_Data(0x11);

    Lcd_Write_Cmd(0xFA);
    Lcd_Write_Data(0x38);
    Lcd_Write_Data(0x20);
    Lcd_Write_Data(0x1C);
    Lcd_Write_Data(0x10);
    Lcd_Write_Data(0x37);
    Lcd_Write_Data(0x12);
    Lcd_Write_Data(0x22);
    Lcd_Write_Data(0x1E);

    Lcd_Write_Cmd(0xC0);//Set GVDD	
    Lcd_Write_Data(0x05);

    Lcd_Write_Cmd(0xC5);//Set Vcom	
    Lcd_Write_Data(0x60);
    Lcd_Write_Data(0x00);

    Lcd_Write_Cmd(0xC7);//Set VCOM-OFFSET	
    Lcd_Write_Data(0xA9);//  可以微调改善flicker

    Lcd_Write_Cmd(0x36);//Memory data  access control
    Lcd_Write_Data(0xC8);//MY MX MV ML RGB MH 0 0   

////Gamma//////////////////
    Lcd_Write_Cmd(0xE0);//E0H Set
    Lcd_Write_Data(0x23);
    Lcd_Write_Data(0x23);
    Lcd_Write_Data(0x24);
    Lcd_Write_Data(0x02);
    Lcd_Write_Data(0x08);
    Lcd_Write_Data(0x0F);
    Lcd_Write_Data(0x35);
    Lcd_Write_Data(0x7B);
    Lcd_Write_Data(0x43);
    Lcd_Write_Data(0x0E);
    Lcd_Write_Data(0x1F);
    Lcd_Write_Data(0x25);
    Lcd_Write_Data(0x10);
    Lcd_Write_Data(0x16);
    Lcd_Write_Data(0x31);

    Lcd_Write_Cmd(0xE1);//E1H Set
    Lcd_Write_Data(0x0D);
    Lcd_Write_Data(0x28);
    Lcd_Write_Data(0x2E);
    Lcd_Write_Data(0x0B);
    Lcd_Write_Data(0x11);
    Lcd_Write_Data(0x12);
    Lcd_Write_Data(0x3E);
    Lcd_Write_Data(0x59);
    Lcd_Write_Data(0x4C);
    Lcd_Write_Data(0x10);
    Lcd_Write_Data(0x26);
    Lcd_Write_Data(0x2B);
    Lcd_Write_Data(0x1B);
    Lcd_Write_Data(0x1B);
    Lcd_Write_Data(0x1B);
    
    Lcd_Write_Cmd(0x29);//display on
    Lcd_Write_Cmd(0x2c);//Memory Write
}



int RGB(int r,int g,int b)
{
	return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}

void LCD_Clear(unsigned int j)                   
{	
  unsigned int i,m;
  Address_set(0,0,320,240);
  Lcd_Write_Cmd(0x02c); //write_memory_start
  LCD_RS_SET;
  LCD_CS_RESET;
  
  for(i=0;i<320;i++)
    for(m=0;m<240;m++)//ff0000
    {   
			//ch=((fcolorr&248)|fcolorg>>5);
			//cl=((fcolorg&28)<<3|fcolorb>>3);

      Lcd_Write_Data(j>>8);
      Lcd_Write_Data(j);

    }
  LCD_CS_SET;   
}


/*

void setup()
{
  for(int p=2;p<10;p++)
  {
    pinMode(p,OUTPUT);
  }
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
  pinMode(A4,OUTPUT);
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  digitalWrite(A4, HIGH);
  Lcd_Init();
//LCD_Clear(0xf800);
//delay(100);
//LCD_Clear(0xf800);
     LCD_Clear( RGB(255,0,0));
         LCD_Clear( RGB(255,255,0));
         LCD_Clear( RGB(255,0,255));
    LCD_Clear( RGB(0,255,255));
    LCD_Clear( RGB(0,255,0));
}

void loop()
{  

       //for(int i=0;i<1000;i++)
     //{ RoundRect(random(240),random(400),random(240),random(400),random(65535));
      // Rect(random(300),random(300),random(300),random(300),random(65535)); // rectangle at x, y, with, hight, color
    // }
     //  LCD_Clear( RGB(255,0,0));
     //    LCD_Clear( RGB(255,255,0));
		 //    LCD_Clear( RGB(255,0,255));
	   //  LCD_Clear( RGB(0,255,255));
	  //   LCD_Clear( RGB(0,255,0));
      
}




*/

